package controller.backOffice;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Qualifier;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;
import org.slf4j.Logger;

import annotation.LoggedIn;
import bean.LoginForm;
import bean.MessageBean;
import bean.RegisterForm;
import ejb.client.ClientService;
import entities.Book;
import entities.Category;
import entities.Client;

@Named
@SessionScoped
public class ClientController implements Serializable {

	private static final long serialVersionUID = 1L;

	//  @ManagedProperty(value="#{param.clientId}")
	//  private Long clientId;

	@Inject
	private LoginForm loginForm;

	@Inject
	private RegisterForm registerForm;

	//@Inject 
	private Client modifClient;
	
	@Inject
	private Logger log;

	@EJB
	private ClientService clientService;

	private StreamedContent photo;
	
	@Inject
	private transient MessageBean messageBean;

	private Client currentClient;

	//private List<Client> clients;


	@Produces @LoggedIn @Named
	public Client getCurrentClient() {
		return currentClient;
	}

	public void setCurrentClient(Client currentClient) {
		this.currentClient = currentClient;
	}

	private UploadedFile file;  
	
	/*@PostConstruct
  public void init() {
	  setClients(clientService.findAll());
  }*/


	public String doLogin() {
		log.debug("doLogin" +loginForm.getLogin() + " "+ loginForm.getPassword());
		currentClient = clientService.login(loginForm.getLogin(), loginForm.getPassword());

		if (currentClient == null) {
			messageBean.addMessage("clientNotFound");
			return null;
		}
		if(currentClient.getRole() == 0) {
			return "welcome";
		} else {
			return "listeBook";
		}
	}

	public String register() {
		currentClient = clientService.register(registerForm.getLogin(), registerForm.getPassword(), registerForm.getNom(),registerForm.getPrenom(), registerForm.getAdresse(), registerForm.getEmail(), registerForm.getTelephone(), 0, registerForm.getPhoto());
		if (currentClient == null) {
			messageBean.addMessage("clientNotFound");
			return null;
		}
		return "welcome";
	}

	public String addClient() {
		return "addNewClient";
	}

	public String delete() { 

		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String clientId1 = (String)requestMap.get("clientId");  

		Long clientId = Long.parseLong(clientId1);
		System.out.println(clientId);

		if (clientId == null ){
			return "welcome";	
		}
		else if (clientId != null){
			clientService.delete(clientId);	
			System.out.println("sucess");
		}
		return "listeClients";
	}

	public String editer(){
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String clientId1 = (String)requestMap.get("clientId");  
		Long clientId = Long.parseLong(clientId1);		
		modifClient=clientService.find(clientId);
		return "editClient";
	}

	public String edit1(){
		Client c = new Client();
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String clientId1 = (String)requestMap.get("clientId");  
		Long clientId = Long.parseLong(clientId1);		
		c=clientService.find(clientId);
		c.setLogin(modifClient.getLogin());
		c.setPassword(modifClient.getPassword()); 
		c.setNom(modifClient.getNom());
		c.setPrenom(modifClient.getPrenom());
		c.setAdresse(modifClient.getAdresse());
		c.setEmail(modifClient.getEmail());
		c.setTelephone(modifClient.getTelephone());
		clientService.update(c);
		return "listeClients";
	}
	
	public String editerProfil(){
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String clientId1 = (String)requestMap.get("clientId");  
		Long clientId = Long.parseLong(clientId1);		
		modifClient=clientService.find(clientId);
		return "editProfil";
	}

	public String editerProfil2(){
		Client c = new Client();
		FacesContext context = FacesContext.getCurrentInstance();  
		Map requestMap = context.getExternalContext().getRequestParameterMap();  
		String clientId1 = (String)requestMap.get("clientId");  
		Long clientId = Long.parseLong(clientId1);		
		c=clientService.find(clientId);
		//c.setLogin(currentClient.getLogin());
		c.setPassword(currentClient.getPassword()); 
		if (null != currentClient.getNom())
		{			
			c.setNom(currentClient.getNom());
		}
		if (null != currentClient.getPrenom()) {
			c.setPrenom(currentClient.getPrenom());
		}
		if (null != currentClient.getAdresse()) {			
			c.setAdresse(currentClient.getAdresse());
		}
		c.setEmail(currentClient.getEmail());
		c.setTelephone(currentClient.getTelephone());
		
		if ( ! file.getContents().equals(null) )
		{
			c.setPhoto(file.getContents());			
		}
		clientService.update(c);
		return "affichageProfil";
	}
	
	public StreamedContent getPhoto() throws IOException {
		FacesContext context = FacesContext.getCurrentInstance();

		if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
			return new DefaultStreamedContent();
		} else {
			Map requestMap = context.getExternalContext().getRequestParameterMap(); 
			String clientId1 = (String)requestMap.get("clientId");  
			Long clientId = Long.parseLong(clientId1);		
			Client c =clientService.find(clientId); 
			return new DefaultStreamedContent(new ByteArrayInputStream(c.getPhoto()));
		}		
	}
	
	public String gestionClient(){
		return "listeClients";
	}

	public boolean isLoggedIn(){
		return currentClient != null;
	}

	public String doLogout() {
		currentClient = null;
		return "welcome";
	}
	
	public String profil(){
		return "profilClient";
	}

	public List<Client> getClients() {
		return clientService.findAll();
	}

	public RegisterForm getRegisterForm() {
		return registerForm;
	}

	public void setRegisterForm(RegisterForm registerForm) {
		this.registerForm = registerForm;
	}

	public Client getModifClient() {
		return modifClient;
	}

	public void setModifClient(Client modifClient) {
		this.modifClient = modifClient;
	}

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}


}